<?php
if (!(isset($through_controller) && $through_controller)) {
    echo '<h1>Errore: non puoi accedere direttamente a questa pagina!</h1>';
    exit();
}
?>
<!DOCTYPE html>

<html lang="it">

    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />

        <title>Eventi Jesolo 2013 - Movimento Giovanile Salesiano</title>

        <link rel="stylesheet" href="<?= Site::APP_URL ?>bootstrap/css/bootstrap.min.css" />
        <link rel="stylesheet" href="<?= Site::APP_URL ?>css/base.css" />
        <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />
        <link rel="stylesheet" href="<?= Site::APP_URL ?>js/timepicker/jquery.ui.timepicker.css" />
        <style>
            form {
                margin-top: 20px;
            }
            form legend {
                margin-bottom: 0px;
            }
        </style>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script src="http://code.jquery.com/ui/1.9.1/jquery-ui.js"></script>
        <script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/rollups/md5.js"></script>
        <script src="<?= Site::APP_URL ?>js/ckeditor/ckeditor.js"></script>
        <script type="text/javascript" src="<?= Site::APP_URL ?>js/ckfinder/ckfinder.js"></script>
        <script type="text/javascript" src="<?= Site::APP_URL ?>js/timepicker/jquery.ui.timepicker.js"></script>
        <script>
            
            $(document).ready(function() {
                $('#get-news-image-btn').click(function(){
                    var finder = new CKFinder();
                    finder.popup();
                    return false;
                });
                CKEDITOR.replace('content', {
                    filebrowserBrowseUrl      : '<?= Site::APP_URL ?>js/ckfinder/ckfinder.html',
                    filebrowserImageBrowseUrl : '<?= Site::APP_URL ?>js/ckfinder/ckfinder.html?type=Images',
                    filebrowserFlashBrowseUrl : '<?= Site::APP_URL ?>js/ckfinder/ckfinder.html?type=Flash',
                    filebrowserUploadUrl      : '<?= Site::APP_URL ?>js/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files',
                    filebrowserImageUploadUrl : '<?= Site::APP_URL ?>js/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images',
                    filebrowserFlashUploadUrl : '<?= Site::APP_URL ?>js/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash',
                    basicEntities : false,
                    clipboard_defaultContentType : 'text',
                    height: 300,
                    toolbar : 'Basic'
                });
                $("#publication_time").timepicker({showPeriodLabels:false,
                    hourText:'Ora',
                    minuteText:'Minuti'
                });
                $("#publication_date").datepicker({dateFormat:'dd/mm/yy',
                    dayNames: [ "Domenica", "Lunedì", "Martedì", "Mercoledì", "Giovedì", "Venerdi", "Sabato" ],
                    monthNames: [ "Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre" ],
                    dayNamesMin: [ "Dom", "Lun", "Mar", "Mer", "Gio", "Ven", "Sab" ],
                    firstDay: 1
                });
                $('#news-form').submit(function(){
                    var error   = false;
                    
                    var title   = $(this).find('#title');
                    var content = $(this).find('#content');
                    
                    var pub_date = $(this).find('#publication_date');
                    var pub_time = $(this).find('#publication_time');
                    var news_img = $(this).find('#image');
                    
                    if (title.val() == '') {
                        error = error || true;
                       
                        $(title).parent().parent().addClass("error");
                        if ($(title).siblings("span.help-inline").length == 0) {
                            $(title).after('<span class="help-inline">Campo Obbligatorio!</span>');
                        }
                    }
                    if (content.val() == '') {
                        error = error || true;
                       
                        $(content).parent().parent().addClass("error");
                        if ($(content).siblings("span.help-inline").length == 0) {
                            $(content).after('<span class="help-inline">Campo Obbligatorio!</span>');
                        }
                    }
                    if (pub_date.val() == '' || pub_time.val() == '') {
                        error = error || true;
                       
                        $(pub_date).parent().parent().addClass("error");
                        if ($(pub_date).siblings("span.help-inline").length == 0) {
                            $(pub_time).after('<span class="help-inline">Campi Obbligatori!</span>');
                        }
                    }
                    if (news_img.val() == '') {
                        error = error || true;
                       
                        $(news_img).parent().parent().addClass("error");
                        if ($(news_img).siblings("span.help-inline").length == 0) {
                            $(news_img).after('<span class="help-inline">Campo Obbligatorio!</span>');
                        }
                    }
                    
                    return !error;
                });
                
                $("#title, #content, #publication_date, #publication_time, #image").focus(function(){
                    $(this).parent().parent().removeClass("error");
                    $(this).parent().find("span.help-inline").remove(); 
                });
            });
        </script>
    </head>
    <body>
        <?= $navigation->to_html(false) ?>
        <!-- Page Layout HTML here -->
        <div class="container">
            <div class="row">
                <div class="span12">
                    <form id="news-form" class="form-horizontal" method="POST" action="<?= Site::APP_URL?>admin/news.php?action=save<?= ($_GET[ACTION] == ACTION_EDIT) ? '&id=' . $news->id : '' ?>">
                        <legend>
                            <?php
                            if ($_GET[ACTION] == ACTION_EDIT) {
                                echo 'Modifica News';
                            } else if ($_GET[ACTION] == ACTION_NEW) {
                                echo 'Nuova News';
                            }
                            ?>
                        </legend>
                        <fieldset>
                            <legend>News</legend>
                            <div class="control-group">
                                <label class="control-label" for="title">Titolo</label>
                                <div class="controls">
                                    <input class="span7" type="text" id="title" placeholder="Titolo" name="title" value="<?= ($_GET[ACTION] == ACTION_EDIT) ? $news->title : '' ?>"/>
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label" for="content">Contenuto</label>
                                <div class="controls">
                                    <textarea id="content" name="content">
                                        <?php 
                                            if ($_GET[ACTION] == ACTION_EDIT) {
                                                echo $news->content;
                                            } else {
                                                echo '&lt;p&gt;Contenuto&lt;/p&gt;';
                                            }
                                        ?>
                                    </textarea>
                                </div>
                            </div>
                        </fieldset>
                        <fieldset>
                            <legend>Informazioni Aggiuntive</legend>
                            <div class="control-group">
                                <label class="control-label" for="publication_date">Data di Pubblicazione</label>
                                <div class="controls">
                                    <input class="span3" type="text" id="publication_date" placeholder="31/01/2013" name="publication_date" value="<?= ($_GET[ACTION] == ACTION_EDIT) ? $news->publication_timestamp->format('d/m/Y') : '' ?>"/>
                                    <input class="span3" type="text" id="publication_time" placeholder="18:00" name="publication_time" value="<?= ($_GET[ACTION] == ACTION_EDIT) ? $news->publication_timestamp->format('H:i') : '' ?>" />
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label" for="priority">Priorità</label>
                                <div class="controls">
                                    <select name="priority">
                                        <option value="1" <?= ($_GET[ACTION] == ACTION_EDIT) ? ($news->priority == 1) ? 'selected' : '' : ''?>>Bassa</option>
                                        <option value="0" <?= ($_GET[ACTION] == ACTION_EDIT) ? ($news->priority == 0) ? 'selected' : '' : ''?>>Alta</option>
                                    </select>
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label" for="links">Link</label>
                                <div class="controls">
                                    <input class="span9" type="text" id="link" placeholder="Link" name="link" value="<?= ($_GET[ACTION] == ACTION_EDIT) ? $news->link : '' ?>" />
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label" for="image">Carica immagini</label>
                                <div class="controls">
                                    <a id="get-news-image-btn" class="btn btn-info" href="#"><i class="icon-upload icon-white"></i> Carica immagini</a>
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label" for="news_image">Immagine News</label>
                                <div class="controls">
                                    <input class="span4" type="text" id="image" placeholder="Immagine News" name="image" value="<?= ($_GET[ACTION] == ACTION_EDIT) ? $news->image : '' ?>" />
                                </div>
                            </div>
                        </fieldset>
                        <div class="control-group">
                            <div class="controls">
                                <button type="submit" name="submit" id="submit" class="btn btn-large btn-success"><i class="icon-white icon-pencil"></i> <?= ($_GET[ACTION] == ACTION_EDIT) ? 'Aggiorna': 'Salva' ?></button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <?= $footer->to_html(false) ?>
        <!-- Initialize JS Plugins -->
        <script src="<?= Site::APP_URL ?>bootstrap/js/bootstrap.min.js"></script>
        <script src="<?= Site::APP_URL ?>js/retina.js"></script>
    </body>
</html>


